package cn.ilovese.collect.serviceimpl.recommend.strategy;

import cn.ilovese.collect.datamapper.user.UserMapper;
import cn.ilovese.collect.po.task.Task;
import cn.ilovese.collect.service.recommend.strategy.RecommendStrategy;
import cn.ilovese.collect.vo.task.TaskRecommendVO;

import java.util.ArrayList;
import java.util.List;

public class LatestDDLFirstStrategy implements RecommendStrategy {
    private static final LatestDDLFirstStrategy INSTANCE=new LatestDDLFirstStrategy();

    @Override
    public List<TaskRecommendVO> recommendExecute(List<Task> allTask,UserMapper userMapper) {
        Double gap= Double.valueOf(1.0/allTask.size());
        int[] rank=new int[allTask.size()];
        List<TaskRecommendVO> res=new ArrayList<>();
        for(int i=0;i<allTask.size();i++){
            System.out.println(userMapper.selectByPrimaryKey(allTask.get(i).getUserId()).getUsername());
            res.add(new TaskRecommendVO(allTask.get(i),userMapper.selectByPrimaryKey(allTask.get(i).getUserId()).getUsername()));
            int rankNow=1;
            for(int j=0;j<allTask.size();j++){
                if(allTask.get(i).getEndTime().compareTo(allTask.get(j).getEndTime())>0)
                    rankNow++;
            }
            rank[i]=rankNow;
        }
        for(int k=0;k< res.size();k++){
            res.get(k).setMatchity(1.0-gap*(rank[k]-1));
        }
        return res;
    }
    private LatestDDLFirstStrategy(){
    }

    public static LatestDDLFirstStrategy getInstance(){
        return INSTANCE;
    }
}
